package com.bdqn.t320.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.t320.pojo.Page;
import com.bdqn.t320.pojo.Role;
import com.bdqn.t320.pojo.SysUser;
import com.bdqn.t320.service.RoleService;
import com.bdqn.t320.service.SysUserService;
import com.bdqn.t320.service.SysUserServiceImp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    SysUserService sysUserService;
    @Resource
    RoleService roleService;

    @RequestMapping("/getUserList")
    public String getUserList(Model model,
                              @RequestParam(value = "userName", required = false) String userName,
                              @RequestParam(value = "userRole", required = false, defaultValue = "0") Integer userRole,
                              @RequestParam(value = "pageIndex", defaultValue = "1") String pageIndex) {
        System.out.println("getUserList");
        SysUser sysUser = new SysUser();
        sysUser.setUserName(userName);
        sysUser.setUserRole(userRole);

        Page page = new Page();
        page.setCurrentPage(Integer.parseInt(pageIndex));
        List<SysUser> userList = sysUserService.getUserListBiz(page, sysUser);
        Integer count = sysUserService.getCountBiz(sysUser);
        //设置总记录数的同时会计算好总页数
        page.setTotalRecord(count);
        page.setList(userList);

        List<Role> roleList = roleService.getRoleListBiz();

        model.addAttribute("page", page);
        model.addAttribute("userName", userName);
        model.addAttribute("userRole", userRole);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("roleList", roleList);
        return "sysUser/list";
    }

    @RequestMapping("/toAdd")
    public String getUserList() {
        System.out.println("toAdd");
        return "sysUser/add";
    }

    @RequestMapping("/add")
    public String add(Model model, SysUser sysUser, HttpSession session) {
        System.out.println("toAdd");
        if (session.getAttribute("sysUser") == null) {
            throw new RuntimeException("请先登录!");
        }
        SysUser user = (SysUser) session.getAttribute("sysUser");
        sysUser.setCreatedBy(user.getId());
        sysUser.setCreationDate(new Date());
        int count = sysUserService.add(sysUser);
        if (count > 0) {
            return "redirect:/user/getUserList";
        } else {
            return "sysUser/add";
        }
    }


    @RequestMapping("/view")
    public String view(Model model, Integer id) {
        System.out.println("view");
        SysUser sysUser = sysUserService.viewBiz(id);
        model.addAttribute("sysUser", sysUser);
        return "sysUser/view";
    }

    //http://localhost:8080/ch11/user/find/24
    @ResponseBody
    @GetMapping(value = "/{id}/find")
    public Object find(Model model, @PathVariable Integer id) {
        System.out.println("find");
        SysUser sysUser = sysUserService.viewBiz(id);
        //model.addAttribute("sysUser", sysUser);
        //return JSON.toJSONString(sysUser);
        return sysUser;
    }

    @PostMapping("/{id}/find")
    public String finda(Model model, @PathVariable Integer id) {
        System.out.println("find");
        SysUser sysUser = sysUserService.viewBiz(id);
        model.addAttribute("sysUser", sysUser);
        return "sysUser/view";
    }
}
